#!/usr/bin/env bash

ps -fe|grep WrongLogic |grep -v grep | grep -v vim
if [ $? -ne 0 ]
then
    CodePath="/root/zkw/project/SimpleMonitor"
    LogPath="${CodePath}/log"
    LogFileName="`date +%Y-%m-%d`.log"

    source /root/Envs/zkw/bin/activate

    cd ${CodePath}

    echo "-----------------`date +%Y-%m-%dT%H:%M:%S` 开始----------------------------" >> $LogPath/$LogFileName
    if [ -n "$1" ] && [ $1 == "unit" ]
    then
        var="unittest"
    else
        cd ./streaming
        var=`date "+%Y-%m-%d-%H-%M-%S"`
        mkdir -p /data/data1/SimpleMongoiter/scrapy/streaming/`date "+%Y"`/`date "+%m"`/`date "+%d"`/`date "+%H"`
        mkdir -p /data/data1/SimpleMongoiter/scrapy/running/`date "+%Y"`/`date "+%m"`/`date "+%d"`/`date "+%H"`
        mkdir -p /data/data1/SimpleMongoiter/scrapy/killed/`date "+%Y"`/`date "+%m"`/`date "+%d"`/`date "+%H"`
        mkdir -p /data/data1/SimpleMongoiter/scrapy/failed/`date "+%Y"`/`date "+%m"`/`date "+%d"`/`date "+%H"`
        {
            echo "爬取streaming信息..." >> $LogPath/$LogFileName
            scrapy crawl ScrapyStreamApp -o /data/data1/SimpleMongoiter/scrapy/streaming/`date "+%Y"`/`date "+%m"`/`date "+%d"`/`date "+%H"`/${var}.json >/dev/null 2>&1
        } &
        {
            echo "爬取running信息..." >> $LogPath/$LogFileName
            scrapy crawl ScrapyRunningApp -o /data/data1/SimpleMongoiter/scrapy/running/`date "+%Y"`/`date "+%m"`/`date "+%d"`/`date "+%H"`/${var}.json >/dev/null 2>&1
        } &
        {
            echo "爬取failed信息..." >> $LogPath/$LogFileName
            scrapy crawl ScrapyFailedApp -o /data/data1/SimpleMongoiter/scrapy/failed/`date "+%Y"`/`date "+%m"`/`date "+%d"`/`date "+%H"`/${var}.json >/dev/null 2>&1
        } &
        {
            echo "爬取killed信息..." >> $LogPath/$LogFileName
            scrapy crawl ScrapyKilledApp -o /data/data1/SimpleMongoiter/scrapy/killed/`date "+%Y"`/`date "+%m"`/`date "+%d"`/`date "+%H"`/${var}.json >/dev/null 2>&1
        } &
        wait
    fi

    cd ../

    wx="--user wxe3591a8b7edc740d --passwd 8R-1XpkI-OqrcioV7BLR7cdhJC1-eOexAqvFtn0Kwpo8N7wkGCCoNMUc7x8ki3I4 --appid 1"
    # task=("streaming" "running" "failed" "zabbix.running" "zabbix.streaming" "quota.monitor" "streaming.monitor" "codis.monitor" "online.monitor")
    task=("streaming" "running" "failed" "zabbix.running" "zabbix.streaming" "streaming.monitor" "codis.monitor" "online.monitor" "cdh.query")
    for field in ${task[@]}
    do
    {
        echo 运行监控: $field >> $LogPath/$LogFileName
        python3 WrongLogic.py --file ${var}.json --mode $field ${wx} 2>&1 >> $LogPath/$LogFileName
    } &
    done

    wx="--user wxe3591a8b7edc740d --passwd vu1MqmUSxHY9TjLKr6p6vOh9oxaEM8RHFZLBJRZWB-9znSwQQ6h2kI8ax6TJ79N8 --appid 1000005"
    task=("wis.raid") # "sdn.spark" 
    for field in ${task[@]}
    do
    {
        echo 运行监控: $field >> $LogPath/$LogFileName
        python3 WrongLogic.py --file ${var}.json --mode $field ${wx} 2>&1 >> $LogPath/$LogFileName
    } &
    done

    # 整合自己的zabbix服务到无线事业部，不需要自己监控
    # python3 WrongLogic.py --file ${var}.json --mode zabbix.monitor ${wx}

    wx="--user wxe3591a8b7edc740d --passwd vu1MqmUSxHY9TjLKr6p6vOh9oxaEM8RHFZLBJRZWB-9znSwQQ6h2kI8ax6TJ79N8 --appid 3"
    {
        echo '运行监控: mongo.replication' >> $LogPath/$LogFileName
        python3 WrongLogic.py --file ${var}.json --mode mongo.replication ${wx} 2>&1 >> $LogPath/$LogFileName
    } &

    currentHour=$(date +%H%M)
    currentSec=$(date +%S)
    echo $currentHour, $currentSec
    if [ $currentHour -eq "0801" ] && [ $currentSec -lt "30" ]
    then
    {
        echo '运行监控: mongo.wis' >> $LogPath/$LogFileName
        python3 WrongLogic.py --file ${var}.json --mode mongo.wis ${wx} 2>&1 >> $LogPath/$LogFileName
    } &
    fi

    wait
    echo '运行采集客户端...' >> $LogPath/$LogFileName
    python3 CollectClient.py --file ${var}.json 2>&1 >> $LogPath/$LogFileName
    echo "-----------------`date +%Y-%m-%dT%H:%M:%S` 结束----------------------------" >> $LogPath/$LogFileName
    echo "" >> $LogPath/$LogFileName

fi
